package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.*;
import com.itheima.pojo.*;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Value("${imgpath.name}")
    public String url;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealCheckgroupMapper sCMapper;

    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Autowired
    private CheckgroupCheckitemMapper cCMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public Result upload(MultipartFile imgFile) throws IOException {
        String lastName = imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        imgFile.transferTo(new File(url + uuid + lastName));
        return new Result(true, "图片上传成功", uuid + lastName);
    }

    @Override
    public Result add(Setmeal setmeal, Integer[] checkgroupIds) {
        //判断参数
        if (setmeal == null) {
            return new Result(false, "参数异常");
        }
        //添加检查套餐
        setmealMapper.insert(setmeal);
        //判断是否勾选检查组
        if (checkgroupIds.length != 0) {
            //如果勾选
            //根据checkgroupIds 获取勾选的检查组id
            for (Integer checkgroupId : checkgroupIds) {
                //添加套餐id和检查组id到数据库联表
                sCMapper.insert(new SetmealCheckgroup(setmeal.getId(), checkgroupId));
            }

        }
        return new Result(true, "添加套餐成功");
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //校验参数
        if (queryPageBean == null) {
            throw new RuntimeException("参数异常");
        }
        //创建Page对象
        IPage<Setmeal> p = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //判断是否有模糊查询
        if (queryPageBean.getQueryString() != null) {
            //如果有 判断条件是 编码、名称、 助记码
            wrapper.eq(Setmeal::getCode, queryPageBean.getQueryString()).or()
                    .like(Setmeal::getName, queryPageBean.getQueryString()).or()
                    .eq(Setmeal::getHelpCode, queryPageBean.getQueryString());
        }
        IPage<Setmeal> setmealIPage = setmealMapper.selectPage(p, wrapper);
        //封装进PageResult对象返回前端
        return new PageResult(setmealIPage.getTotal(), setmealIPage.getRecords());
    }

    @Override
    public Result getAllSetmeal() {
        List<Setmeal> setmeals = setmealMapper.selectList(null);
        return new Result(true, "查询套餐列表成功", setmeals);
    }

    @Override
    public Result findById(Integer id) {
        //查询套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据套餐id 查询对应检查组的ids
        LambdaQueryWrapper<SetmealCheckgroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealCheckgroup::getSetmealId, id);
        List<SetmealCheckgroup> setmealCheckgroupList = sCMapper.selectList(wrapper);

        ArrayList<Integer> checkGroupIds = new ArrayList<>();
        setmealCheckgroupList.forEach(c -> {
            checkGroupIds.add(c.getCheckgroupId());
        });
        System.out.println("套餐对应检查组id  "+checkGroupIds);
        //封装套餐及对应检查组信息到setmealDto对象
        List<CheckGroup> checkGroupList = checkgroupMapper.selectBatchIds(checkGroupIds);

        ArrayList<Integer> checkItemIds = new ArrayList<>();
        for (CheckGroup checkGroup : checkGroupList) {
            LambdaQueryWrapper<CheckgroupCheckitem> cCWrapper = new LambdaQueryWrapper<>();
            cCWrapper.eq(CheckgroupCheckitem::getCheckgroupId,checkGroup.getId());
            List<CheckgroupCheckitem> cCList = cCMapper.selectList(cCWrapper);
            for (CheckgroupCheckitem checkgroupCheckitem : cCList) {
                checkItemIds.add(checkgroupCheckitem.getCheckitemId());
            }
            List<CheckItem> checkItems = checkItemMapper.selectBatchIds(checkItemIds);
            checkGroup.setCheckItems(checkItems);
        }

        setmeal.setCheckGroups(checkGroupList);

        //根据检查组的ids 查询出对应的检查项信息

        return new Result(true, "查询成功", setmeal);
    }
}
